package org.dromara.job.snailjob;


import cn.hutool.core.collection.CollectionUtil;
import com.aizuda.snailjob.client.job.core.annotation.JobExecutor;
import com.aizuda.snailjob.client.job.core.dto.JobArgs;
import com.aizuda.snailjob.client.model.ExecuteResult;
import com.aizuda.snailjob.common.core.util.JsonUtil;
import com.aizuda.snailjob.common.log.SnailJobLog;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.dromara.dao.business.domain.Settings;
import org.dromara.dao.business.domain.User;
import org.dromara.dao.business.domain.UserBalanceLog;
import org.dromara.dao.business.mapper.SettingsMapper;
import org.dromara.dao.business.mapper.UserBalanceLogMapper;
import org.dromara.dao.business.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Component
@JobExecutor(name = "balanceResetJobExecutor")
@Slf4j
public class BalanceResetJobExecutor {

    @Autowired
    UserMapper userMapper;
    @Autowired
    SettingsMapper settingsMapper;
    @Autowired
    UserBalanceLogMapper userBalanceLogMapper;



    public ExecuteResult jobExecute(JobArgs jobArgs) {
        SnailJobLog.LOCAL.info("balanceResetJobExecutor. jobArgs:{}", JsonUtil.toJsonString(jobArgs));
        SnailJobLog.REMOTE.info("balanceResetJobExecutor. jobArgs:{}", JsonUtil.toJsonString(jobArgs));

        Integer resetDiamond = 0;
        Integer resetStar = 0;

        List<Settings> settingsList = settingsMapper.selectList(new LambdaQueryWrapper<>());
        if (CollectionUtil.isNotEmpty(settingsList)) {
            for (Settings settings : settingsList) {
                if ("common_reset_diamond_count".equals(settings.getSettingKey())) {
                    resetDiamond = Integer.parseInt(settings.getSettingValue());
                } else if ("common_reset_star_count".equals(settings.getSettingKey())) {
                    resetStar = Integer.parseInt(settings.getSettingValue());
                }
            }
        }

        List<User> users = userMapper.selectList(new LambdaQueryWrapper<User>().lt(User::getBalanceDiamond, resetDiamond).or().lt(User::getBalanceStar, resetStar));
        if (CollectionUtil.isNotEmpty(users)) {
            for (User user : users) {
                try {
                    resetUserBalance(user, resetDiamond, resetStar);
                } catch (Exception e) {

                }
            }
        }


        return ExecuteResult.success("测试成功");
    }

    @Transactional(rollbackFor = Exception.class)
    public void resetUserBalance(User user, Integer diamond, Integer star) {
        UserBalanceLog userBalanceLog = new UserBalanceLog();
        userBalanceLog.setUserId(user.getId());
        userBalanceLog.setDiamondBefore(user.getBalanceDiamond());
        userBalanceLog.setStarBefore(user.getBalanceStar());
        userBalanceLog.setRemark("每天积分重置");
        if (user.getBalanceDiamond()< diamond) {
            user.setBalanceDiamond(diamond);
        }
        if (user.getBalanceStar()<star) {
            user.setBalanceStar(star);
        }

        userBalanceLog.setDiamondAfter(user.getBalanceDiamond());
        userBalanceLog.setStarAfter(user.getBalanceStar());
        userBalanceLog.setDiamondDelta(user.getBalanceDiamond() - userBalanceLog.getDiamondBefore());
        userBalanceLog.setStarDelta(user.getBalanceStar() - userBalanceLog.getStarBefore());
        userBalanceLogMapper.insert(userBalanceLog);
        userMapper.updateById(user);
    }
}
